package o;

import androidx.recyclerview.widget.RecyclerView.s;
import java.util.ArrayList;
import java.util.List;
import o.hg.c;

public class mc implements c {
    final mm ʻ;
    private o.hg.e<a> ʽ;
    final ArrayList<a> ˊ;
    final e ˋ;
    Runnable ˎ;
    final ArrayList<a> ˏ;
    final boolean ॱ;
    private int ॱॱ;

    public static class a {
        public int ˊ;
        public Object ˋ;
        public int ˎ;
        public int ॱ;

        a(int i, int i2, int i3, Object obj) {
            this.ˊ = i;
            this.ॱ = i2;
            this.ˎ = i3;
            this.ˋ = obj;
        }

        String ˏ() {
            switch (this.ˊ) {
                case 1:
                    return "add";
                case 2:
                    return "rm";
                case 4:
                    return "up";
                case 8:
                    return "mv";
                default:
                    return "??";
            }
        }

        public String toString() {
            return Integer.toHexString(System.identityHashCode(this)) + "[" + ˏ() + ",s:" + this.ॱ + "c:" + this.ˎ + ",p:" + this.ˋ + "]";
        }

        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null || getClass() != obj.getClass()) {
                return false;
            }
            a aVar = (a) obj;
            if (this.ˊ != aVar.ˊ) {
                return false;
            }
            if (this.ˊ == 8 && Math.abs(this.ˎ - this.ॱ) == 1 && this.ˎ == aVar.ॱ && this.ॱ == aVar.ˎ) {
                return true;
            }
            if (this.ˎ != aVar.ˎ) {
                return false;
            }
            if (this.ॱ != aVar.ॱ) {
                return false;
            }
            if (this.ˋ != null) {
                if (this.ˋ.equals(aVar.ˋ)) {
                    return true;
                }
                return false;
            } else if (aVar.ˋ != null) {
                return false;
            } else {
                return true;
            }
        }

        public int hashCode() {
            return (((this.ˊ * 31) + this.ॱ) * 31) + this.ˎ;
        }
    }

    public interface e {
        void ˋ(int i, int i2);

        s ˎ(int i);

        void ˎ(int i, int i2);

        void ˎ(int i, int i2, Object obj);

        void ˏ(int i, int i2);

        void ˏ(a aVar);

        void ॱ(int i, int i2);

        void ॱ(a aVar);
    }

    public mc(e eVar) {
        this(eVar, false);
    }

    mc(e eVar, boolean z) {
        this.ʽ = new c(30);
        this.ˏ = new ArrayList();
        this.ˊ = new ArrayList();
        this.ॱॱ = 0;
        this.ˋ = eVar;
        this.ॱ = z;
        this.ʻ = new mm(this);
    }

    public void ˊ() {
        ˏ(this.ˏ);
        ˏ(this.ˊ);
        this.ॱॱ = 0;
    }

    public void ˋ() {
        this.ʻ.ˊ(this.ˏ);
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            a aVar = (a) this.ˏ.get(i);
            switch (aVar.ˊ) {
                case 1:
                    ᐝ(aVar);
                    break;
                case 2:
                    ˊ(aVar);
                    break;
                case 4:
                    ॱ(aVar);
                    break;
                case 8:
                    ˏ(aVar);
                    break;
            }
            if (this.ˎ != null) {
                this.ˎ.run();
            }
        }
        this.ˏ.clear();
    }

    public void ˏ() {
        int size = this.ˊ.size();
        for (int i = 0; i < size; i++) {
            this.ˋ.ॱ((a) this.ˊ.get(i));
        }
        ˏ(this.ˊ);
        this.ॱॱ = 0;
    }

    private void ˏ(a aVar) {
        ॱॱ(aVar);
    }

    private void ˊ(a aVar) {
        int i = aVar.ॱ;
        int i2 = aVar.ॱ + aVar.ˎ;
        Object obj = -1;
        int i3 = aVar.ॱ;
        int i4 = 0;
        while (i3 < i2) {
            Object obj2;
            int i5;
            if (this.ˋ.ˎ(i3) != null || ॱ(i3)) {
                if (obj == null) {
                    ˋ(ˊ(2, i, i4, null));
                    obj2 = 1;
                } else {
                    obj2 = null;
                }
                obj = 1;
            } else {
                if (obj == 1) {
                    ॱॱ(ˊ(2, i, i4, null));
                    obj2 = 1;
                } else {
                    obj2 = null;
                }
                obj = null;
            }
            if (obj2 != null) {
                i5 = i3 - i4;
                i3 = i2 - i4;
                i2 = 1;
            } else {
                int i6 = i3;
                i3 = i2;
                i2 = i4 + 1;
                i5 = i6;
            }
            i4 = i2;
            i2 = i3;
            i3 = i5 + 1;
        }
        if (i4 != aVar.ˎ) {
            ˎ(aVar);
            aVar = ˊ(2, i, i4, null);
        }
        if (obj == null) {
            ˋ(aVar);
        } else {
            ॱॱ(aVar);
        }
    }

    private void ॱ(a aVar) {
        int i = aVar.ॱ;
        int i2 = aVar.ॱ + aVar.ˎ;
        int i3 = aVar.ॱ;
        Object obj = -1;
        int i4 = 0;
        while (i3 < i2) {
            int i5;
            Object obj2;
            if (this.ˋ.ˎ(i3) != null || ॱ(i3)) {
                if (obj == null) {
                    ˋ(ˊ(4, i, i4, aVar.ˋ));
                    i4 = 0;
                    i = i3;
                }
                i5 = i;
                i = i4;
                obj2 = 1;
            } else {
                if (obj == 1) {
                    ॱॱ(ˊ(4, i, i4, aVar.ˋ));
                    i4 = 0;
                    i = i3;
                }
                i5 = i;
                i = i4;
                obj2 = null;
            }
            i3++;
            Object obj3 = obj2;
            i4 = i + 1;
            i = i5;
            obj = obj3;
        }
        if (i4 != aVar.ˎ) {
            Object obj4 = aVar.ˋ;
            ˎ(aVar);
            aVar = ˊ(4, i, i4, obj4);
        }
        if (obj == null) {
            ˋ(aVar);
        } else {
            ॱॱ(aVar);
        }
    }

    private void ˋ(a aVar) {
        if (aVar.ˊ == 1 || aVar.ˊ == 8) {
            throw new IllegalArgumentException("should not dispatch add or move for pre layout");
        }
        int i;
        int ˎ = ˎ(aVar.ॱ, aVar.ˊ);
        int i2 = aVar.ॱ;
        switch (aVar.ˊ) {
            case 2:
                i = 0;
                break;
            case 4:
                i = 1;
                break;
            default:
                throw new IllegalArgumentException("op should be remove or update." + aVar);
        }
        int i3 = 1;
        int i4 = ˎ;
        ˎ = i2;
        for (i2 = 1; i2 < aVar.ˎ; i2++) {
            Object obj;
            int ˎ2 = ˎ(aVar.ॱ + (i * i2), aVar.ˊ);
            int i5;
            switch (aVar.ˊ) {
                case 2:
                    if (ˎ2 != i4) {
                        obj = null;
                        break;
                    } else {
                        i5 = 1;
                        break;
                    }
                case 4:
                    if (ˎ2 != i4 + 1) {
                        obj = null;
                        break;
                    } else {
                        i5 = 1;
                        break;
                    }
                default:
                    obj = null;
                    break;
            }
            if (obj != null) {
                i3++;
            } else {
                a ˊ = ˊ(aVar.ˊ, i4, i3, aVar.ˋ);
                ˏ(ˊ, ˎ);
                ˎ(ˊ);
                if (aVar.ˊ == 4) {
                    ˎ += i3;
                }
                i3 = 1;
                i4 = ˎ2;
            }
        }
        Object obj2 = aVar.ˋ;
        ˎ(aVar);
        if (i3 > 0) {
            a ˊ2 = ˊ(aVar.ˊ, i4, i3, obj2);
            ˏ(ˊ2, ˎ);
            ˎ(ˊ2);
        }
    }

    void ˏ(a aVar, int i) {
        this.ˋ.ˏ(aVar);
        switch (aVar.ˊ) {
            case 2:
                this.ˋ.ˏ(i, aVar.ˎ);
                return;
            case 4:
                this.ˋ.ˎ(i, aVar.ˎ, aVar.ˋ);
                return;
            default:
                throw new IllegalArgumentException("only remove and update ops can be dispatched in first pass");
        }
    }

    private int ˎ(int i, int i2) {
        int i3;
        int i4 = i;
        for (int size = this.ˊ.size() - 1; size >= 0; size--) {
            a aVar = (a) this.ˊ.get(size);
            if (aVar.ˊ == 8) {
                int i5;
                int i6;
                if (aVar.ॱ < aVar.ˎ) {
                    i5 = aVar.ॱ;
                    i3 = aVar.ˎ;
                } else {
                    i5 = aVar.ˎ;
                    i3 = aVar.ॱ;
                }
                if (i4 < i5 || i4 > r2) {
                    if (i4 < aVar.ॱ) {
                        if (i2 == 1) {
                            aVar.ॱ++;
                            aVar.ˎ++;
                            i6 = i4;
                        } else if (i2 == 2) {
                            aVar.ॱ--;
                            aVar.ˎ--;
                        }
                    }
                    i6 = i4;
                } else if (i5 == aVar.ॱ) {
                    if (i2 == 1) {
                        aVar.ˎ++;
                    } else if (i2 == 2) {
                        aVar.ˎ--;
                    }
                    i6 = i4 + 1;
                } else {
                    if (i2 == 1) {
                        aVar.ॱ++;
                    } else if (i2 == 2) {
                        aVar.ॱ--;
                    }
                    i6 = i4 - 1;
                }
                i4 = i6;
            } else if (aVar.ॱ <= i4) {
                if (aVar.ˊ == 1) {
                    i4 -= aVar.ˎ;
                } else if (aVar.ˊ == 2) {
                    i4 += aVar.ˎ;
                }
            } else if (i2 == 1) {
                aVar.ॱ++;
            } else if (i2 == 2) {
                aVar.ॱ--;
            }
        }
        for (i3 = this.ˊ.size() - 1; i3 >= 0; i3--) {
            aVar = (a) this.ˊ.get(i3);
            if (aVar.ˊ == 8) {
                if (aVar.ˎ == aVar.ॱ || aVar.ˎ < 0) {
                    this.ˊ.remove(i3);
                    ˎ(aVar);
                }
            } else if (aVar.ˎ <= 0) {
                this.ˊ.remove(i3);
                ˎ(aVar);
            }
        }
        return i4;
    }

    private boolean ॱ(int i) {
        int size = this.ˊ.size();
        for (int i2 = 0; i2 < size; i2++) {
            a aVar = (a) this.ˊ.get(i2);
            if (aVar.ˊ == 8) {
                if (ˊ(aVar.ˎ, i2 + 1) == i) {
                    return true;
                }
            } else if (aVar.ˊ == 1) {
                int i3 = aVar.ॱ + aVar.ˎ;
                for (int i4 = aVar.ॱ; i4 < i3; i4++) {
                    if (ˊ(i4, i2 + 1) == i) {
                        return true;
                    }
                }
                continue;
            } else {
                continue;
            }
        }
        return false;
    }

    private void ᐝ(a aVar) {
        ॱॱ(aVar);
    }

    private void ॱॱ(a aVar) {
        this.ˊ.add(aVar);
        switch (aVar.ˊ) {
            case 1:
                this.ˋ.ॱ(aVar.ॱ, aVar.ˎ);
                return;
            case 2:
                this.ˋ.ˋ(aVar.ॱ, aVar.ˎ);
                return;
            case 4:
                this.ˋ.ˎ(aVar.ॱ, aVar.ˎ, aVar.ˋ);
                return;
            case 8:
                this.ˋ.ˎ(aVar.ॱ, aVar.ˎ);
                return;
            default:
                throw new IllegalArgumentException("Unknown update op type for " + aVar);
        }
    }

    public boolean ˎ() {
        return this.ˏ.size() > 0;
    }

    public boolean ˏ(int i) {
        return (this.ॱॱ & i) != 0;
    }

    public int ˊ(int i) {
        return ˊ(i, 0);
    }

    int ˊ(int i, int i2) {
        int size = this.ˊ.size();
        int i3 = i;
        while (i2 < size) {
            a aVar = (a) this.ˊ.get(i2);
            if (aVar.ˊ == 8) {
                if (aVar.ॱ == i3) {
                    i3 = aVar.ˎ;
                } else {
                    if (aVar.ॱ < i3) {
                        i3--;
                    }
                    if (aVar.ˎ <= i3) {
                        i3++;
                    }
                }
            } else if (aVar.ॱ > i3) {
                continue;
            } else if (aVar.ˊ == 2) {
                if (i3 < aVar.ॱ + aVar.ˎ) {
                    return -1;
                }
                i3 -= aVar.ˎ;
            } else if (aVar.ˊ == 1) {
                i3 += aVar.ˎ;
            }
            i2++;
        }
        return i3;
    }

    public boolean ˊ(int i, int i2, Object obj) {
        boolean z = true;
        if (i2 < 1) {
            return false;
        }
        this.ˏ.add(ˊ(4, i, i2, obj));
        this.ॱॱ |= 4;
        if (this.ˏ.size() != 1) {
            z = false;
        }
        return z;
    }

    public boolean ˏ(int i, int i2) {
        boolean z = true;
        if (i2 < 1) {
            return false;
        }
        this.ˏ.add(ˊ(1, i, i2, null));
        this.ॱॱ |= 1;
        if (this.ˏ.size() != 1) {
            z = false;
        }
        return z;
    }

    public boolean ˋ(int i, int i2) {
        boolean z = true;
        if (i2 < 1) {
            return false;
        }
        this.ˏ.add(ˊ(2, i, i2, null));
        this.ॱॱ |= 2;
        if (this.ˏ.size() != 1) {
            z = false;
        }
        return z;
    }

    public boolean ˋ(int i, int i2, int i3) {
        boolean z = true;
        if (i == i2) {
            return false;
        }
        if (i3 != 1) {
            throw new IllegalArgumentException("Moving more than 1 item is not supported yet");
        }
        this.ˏ.add(ˊ(8, i, i2, null));
        this.ॱॱ |= 8;
        if (this.ˏ.size() != 1) {
            z = false;
        }
        return z;
    }

    public void ॱ() {
        ˏ();
        int size = this.ˏ.size();
        for (int i = 0; i < size; i++) {
            a aVar = (a) this.ˏ.get(i);
            switch (aVar.ˊ) {
                case 1:
                    this.ˋ.ॱ(aVar);
                    this.ˋ.ॱ(aVar.ॱ, aVar.ˎ);
                    break;
                case 2:
                    this.ˋ.ॱ(aVar);
                    this.ˋ.ˏ(aVar.ॱ, aVar.ˎ);
                    break;
                case 4:
                    this.ˋ.ॱ(aVar);
                    this.ˋ.ˎ(aVar.ॱ, aVar.ˎ, aVar.ˋ);
                    break;
                case 8:
                    this.ˋ.ॱ(aVar);
                    this.ˋ.ˎ(aVar.ॱ, aVar.ˎ);
                    break;
            }
            if (this.ˎ != null) {
                this.ˎ.run();
            }
        }
        ˏ(this.ˏ);
        this.ॱॱ = 0;
    }

    public int ˋ(int i) {
        int size = this.ˏ.size();
        int i2 = i;
        for (int i3 = 0; i3 < size; i3++) {
            a aVar = (a) this.ˏ.get(i3);
            switch (aVar.ˊ) {
                case 1:
                    if (aVar.ॱ > i2) {
                        break;
                    }
                    i2 += aVar.ˎ;
                    break;
                case 2:
                    if (aVar.ॱ <= i2) {
                        if (aVar.ॱ + aVar.ˎ <= i2) {
                            i2 -= aVar.ˎ;
                            break;
                        }
                        return -1;
                    }
                    continue;
                case 8:
                    if (aVar.ॱ != i2) {
                        if (aVar.ॱ < i2) {
                            i2--;
                        }
                        if (aVar.ˎ > i2) {
                            break;
                        }
                        i2++;
                        break;
                    }
                    i2 = aVar.ˎ;
                    break;
                default:
                    break;
            }
        }
        return i2;
    }

    public boolean ॱॱ() {
        return (this.ˊ.isEmpty() || this.ˏ.isEmpty()) ? false : true;
    }

    public a ˊ(int i, int i2, int i3, Object obj) {
        a aVar = (a) this.ʽ.ˎ();
        if (aVar == null) {
            return new a(i, i2, i3, obj);
        }
        aVar.ˊ = i;
        aVar.ॱ = i2;
        aVar.ˎ = i3;
        aVar.ˋ = obj;
        return aVar;
    }

    public void ˎ(a aVar) {
        if (!this.ॱ) {
            aVar.ˋ = null;
            this.ʽ.ˋ(aVar);
        }
    }

    void ˏ(List<a> list) {
        int size = list.size();
        for (int i = 0; i < size; i++) {
            ˎ((a) list.get(i));
        }
        list.clear();
    }
}
